package com.lzpu.thymeleafproject.demos.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledTaskService {
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private SendEmailService sendEmailService;
//    private static final SimpleDateFormat dateFormat =
//            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//    private Integer count1 = 1;
//    private Integer count2 = 1;
//    private Integer count3 = 1;
//    @Scheduled(fixedRate = 60000)
//    public void scheduledTaskImmediately() {
//        System.out.println(String.format("fixedRate第%s次执行，当前时间为：%s",
//                count1++, dateFormat.format(new Date())));
//    }
//
//    @Scheduled(fixedDelay = 60000)
//    public void scheduledTaskAfterSleep() throws InterruptedException {
//        System.out.println(String.format("fixedDelay第%s次执行，当前时间为：%s",
//                count2++, dateFormat.format(new Date())));
//        Thread.sleep(10000);
//    }
//    @Scheduled(cron = "0 * * * * *")
//    public void scheduledTaskCron(){
//        System.out.println(String.format("cron第%s次执行，当前时间为：%s",
//                count3++, dateFormat.format(new Date())));
//    }

    @Scheduled(cron = "0 30 11 * * *")
    public void scheduledTaskEmail(){
        String to="707333732@qq.com";
        String subject="【模板邮件】HTML模版文件";
        Context context = new Context();
        context.setVariable("username", "石头");
        context.setVariable("code", "123456");
        String emailContent = templateEngine.process("sign_up", context);
        sendEmailService.sendTemplateEmail(to,subject,emailContent);
    }
}
